using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;
using System.Text.RegularExpressions;
using OgilvyOne.Utility;

namespace OgilvyOne.SqlServerDAL
{
    public class ContentTagClass : Icontenttag
    {
        // Methods
        public void DeleteTag(int con_id)
        {
            string cmdText = "delete column_tbl where con_id=" + con_id;
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        public contenttagInfo getTagInfo(int column_id)
        {
            string cmdText = "SELECT column_id,con_id,column_name,is_view,seq FROM column_tbl where column_id=" + column_id;
            SqlParameter[] cmdParms = new SqlParameter[0];
            contenttagInfo info = null;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    info = new contenttagInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetString(3), reader.GetInt32(4));
                }
            }
            return info;
        }

        public IList getTagList(int con_id)
        {
            string cmdText = "SELECT column_id,con_id,column_name,is_view,seq FROM column_tbl where con_id=" + con_id + " order by seq";
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    contenttagInfo info = new contenttagInfo(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetString(3), reader.GetInt32(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public void Insert(contenttagInfo taginfo)
        {
            string cmdText = string.Concat(new object[] { "INSERT INTO column_tbl(column_id,con_id,column_name,is_view,seq) VALUES(", taginfo.column_id, ",", taginfo.con_id, ",'", StringUtil.ReplaceStr(taginfo.column_name), "','", taginfo.is_view, "',", taginfo.seq, ")" });
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        public void Update(contenttagInfo taginfo)
        {
            string cmdText = string.Concat(new object[] { "UPDATE column_tbl SET con_id = ", taginfo.con_id, ",column_name = ", taginfo.column_name, ",is_view = '", taginfo.is_view, "',seq = ", taginfo.seq, ", WHERE column_id =", taginfo.column_id });
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}